
#include "UT_Case.h"
#include "cc_pub.h"

#include "cc_malloc.h"
#include "GDLink.h"

#include "FQueue.h"
#include "ByteFIFO.h"
#include "PkgQueue.h"
#include "MsgQueue.h"
#include "DLMsgQueue.h"

// #include "ObjPool.h"
#include "SortTimer.h"

/********* interior	define ***********/

#define LLOG
#define LLOG_ERR
#define LLOG_MARK
#define LLOG_TICK
#include "llog.h"

#ifdef	CFG_STATIC_BUF_ALLOCATE
#define L_MALLOC 	SMalloc
#define L_FREE(A) 	LLOG_ERR()
#else
#define L_MALLOC 	malloc
#define L_FREE(A)	free(A)
#endif
/********** global val ***************/

/******* local function **************/

/******* TODO ************************/


//=============
#ifdef CFG_UT_MODE_FQUEUE 	// ok

typedef struct FQueTest{
	
	uint8_t a;
	uint8_t b;
	uint32_t c;
}tFQTest;

static tFQTest Test[]={
	{1,2,10},
	{3,4,20},
};

void Show_FQTest(tFQTest* pObj){
	LLOG("a=[%d]\nb=[%d]\nc=[%d]\n",pObj->a,pObj->b,pObj->c);
}


#define UT_DEQUE() 	do{\
	LLOG("\n======\n");\
	ret = FQueue_Deque(ptmp,sizeof(tFQTest),(uint8_t*)&RBuf);\
	UT_CHECK(ret > 0);\
	Show_FQTest(&RBuf);\
}while(0);

int UT_FQueue(void){
	
	uint32_t ret;
	tFQTest RBuf;
	tFQTest RBuf2[2];
	tFQueue* ptmp;
	int i;
	
	LLOG_MARK();
	
	ptmp = FQueue_Create(10,sizeof(tFQTest));
	FQueue_ShowObj(ptmp);
	
	#if 1
	LLOG("\n====== Test RW\n");
	FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[0]);
	FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[1]);
	FQueue_ShowObj(ptmp);
	
	UT_DEQUE();
	UT_DEQUE();
	
	LLOG("\n======\n");
	FQueue_ShowObj(ptmp);
	
	#endif
	
	#if 1
	LLOG("\n====== Test RW Loop\n");
	FQueue_ShowObj(ptmp);
	for(i=0;i<9;i++){
		if(i&0x01){
			FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[0]);
		}else{
			FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[1]);
		}
		ret = FQueue_Deque(ptmp,sizeof(tFQTest),(uint8_t*)&RBuf);
	}
	LLOG("\n======\n");
	FQueue_ShowObj(ptmp);	
	#endif
	
	#if 1
	LLOG("\n====== Test OverRW\n");
	FQueue_ShowObj(ptmp);
	for(i=0;i<11;i++){
		if(i&0x01){
			FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[0]);
		}else{
			FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[1]);
		}
	}
	LLOG("\n======\n");
	FQueue_ShowObj(ptmp);
	for(i=0;i<11;i++){
		ret = FQueue_Deque(ptmp,sizeof(tFQTest),(uint8_t*)&RBuf);
		if(ret > 0)
			Show_FQTest(&RBuf);
	}
	
	#endif

	#if 1
	LLOG("\n====== Test ShortR\n");
	// FQueue_ShowObj(ptmp);
	FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[0]);
	ret = FQueue_Deque(ptmp,sizeof(tFQTest)-4,(uint8_t*)&RBuf);
	LLOG("rRet=[%d]\n",ret);
	UT_CHECK(ret == (sizeof(tFQTest)-4)); // 截短copy
	LLOG("\n======\n");
	// FQueue_ShowObj(ptmp);
	#endif
	
	#if 1
	LLOG("\n====== Test LongR\n");
	// FQueue_ShowObj(ptmp);
	FQueue_Enque(ptmp,sizeof(tFQTest),(uint8_t*)&Test[0]);
	ret = FQueue_Deque(ptmp,sizeof(tFQTest)+4,(uint8_t*)&RBuf2[0]);
	LLOG("rRet=[%d]\n",ret);
	UT_CHECK(ret == sizeof(tFQTest)); // 截短copy
	LLOG("\n======\n");
	// FQueue_ShowObj(ptmp);
	#endif	
	
	#if 1
	LLOG("\n====== Test ShortW\n");
	// FQueue_ShowObj(ptmp);
	ret = FQueue_Enque(ptmp,sizeof(tFQTest)-4,(uint8_t*)&Test[0]);
	LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == (sizeof(tFQTest)-4));
	ret = FQueue_Deque(ptmp,sizeof(tFQTest),(uint8_t*)&RBuf);
	LLOG("rRet=[%d]\n",ret);
	UT_CHECK(ret == sizeof(tFQTest));
	
	LLOG("\n======\n");
	// FQueue_ShowObj(ptmp);
	#endif
	
	#if 1
	LLOG("\n====== Test LongW\n");
	// FQueue_ShowObj(ptmp);
	ret = FQueue_Enque(ptmp,sizeof(tFQTest)+4,(uint8_t*)&Test[0]);
	LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == sizeof(tFQTest));
	ret = FQueue_Deque(ptmp,sizeof(tFQTest),(uint8_t*)&RBuf);
	LLOG("rRet=[%d]\n",ret);
	UT_CHECK(ret == sizeof(tFQTest));
	
	LLOG("\n======\n");
	// FQueue_ShowObj(ptmp);
	#endif	

}


#endif

#ifdef CFG_UT_BYTE_FIFO

#define TEST_RBUF_SZ 10
int UT_BFIFO(void){
	
	uint32_t ret;
	uint8_t TestStr[]="123456789abcdefg";
	uint8_t RBuf[TEST_RBUF_SZ+10];
	uint32_t dLen;
	uint32_t RealNum;
	tBFIFO* ptmp;
	
	LLOG_MARK();
	ptmp = BFIFO_Create(TEST_RBUF_SZ);
	BFIFO_ShowObj(ptmp);
	
	
	#if 1
	LLOG("\n====== Test single RW\n");
	// 写入一段数据，然后完全读出
	BFIFO_Reset(ptmp);
	dLen = 5;// sizeof(TestStr)-1;
	
	BFIFO_Write(ptmp,dLen,&TestStr[0]);
	RealNum=BFIFO_Read(ptmp,sizeof(RBuf),&RBuf[0]);
	LLOG("RealNum=[%d]\n",RealNum);
	UT_CHECK(RealNum == dLen);
	BFIFO_ShowObj(ptmp);
	LLOG("\n======\n");
	#endif
	
	#if 1
	LLOG("\n====== Test overRW\n");
	// 写入一段数据，然后完全读出
	// BFIFO_Reset(ptmp);
	dLen = 7;// sizeof(TestStr)-1;
	
	BFIFO_Write(ptmp,dLen,&TestStr[0]);
	MEMSET(&RBuf[0],0,sizeof(RBuf));
	RealNum=BFIFO_Read(ptmp,sizeof(RBuf),&RBuf[0]);
	LLOG("RealNum=[%d]\n",RealNum);
	UT_CHECK(RealNum == dLen);
	Log_ShowArray(NULL,RealNum,&RBuf[0]);
	BFIFO_ShowObj(ptmp);
	LLOG("\n======\n");
	#endif
	
	#if 1
	LLOG("\n====== Test overW short cut\n");
	dLen = 12;// sizeof(TestStr)-1;
	
	ret = BFIFO_Write(ptmp,dLen,&TestStr[0]);
	// LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == TEST_RBUF_SZ-1);
	BFIFO_ShowObj(ptmp);
	LLOG("\n======\n");
	#endif	
	
	#if 1
	LLOG("\n====== Test overW short cut\n");
	BFIFO_Reset(ptmp);
	BFIFO_ShowObj(ptmp);
	
	dLen = 12;// sizeof(TestStr)-1;
	ret = BFIFO_Write(ptmp,dLen,&TestStr[0]);
	// LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == TEST_RBUF_SZ-1);
	BFIFO_ShowObj(ptmp);
	LLOG("\n======\n");
	#endif	
	
	#if 1
	LLOG("\n====== Test PartRead\n");
	BFIFO_Reset(ptmp);
	// BFIFO_ShowObj(ptmp);
	dLen = 7;
	ret = BFIFO_Write(ptmp,dLen,&TestStr[0]);
	
	MEMSET(&RBuf[0],0,sizeof(RBuf));
	ret=BFIFO_Read(ptmp,2,&RBuf[0]);
	LLOG("rRet=[%d]\n",ret);
	UT_CHECK(ret == 2);
	Log_ShowArray(NULL,ret,&RBuf[0]);
	
	BFIFO_ShowObj(ptmp);
	LLOG("\n======\n");	
	#endif
	
	return ret;
}

#endif

#ifdef CFG_UT_PKG_QUEUE

#define TEST_RBUF_SZ 20
int UT_PkgQueue(void){
	
	uint32_t ret;
	uint8_t TestStr[]="123456789abcdefg";
	uint8_t RBuf[TEST_RBUF_SZ+256];
	uint32_t dLen;
	uint32_t RealNum;
	tPkgQueue* ptmp;
	
	int i;
	
	LLOG_MARK();
	ptmp = PkgQueue_Create(TEST_RBUF_SZ);
	PkgQueue_ShowObj(ptmp);
	
	#if 1
	LLOG("\n====== Test single RW\n");
	PkgQueue_Reset(ptmp);
	dLen = 8;
	
	ret = PkgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	
	MEMSET(&RBuf[0],0,sizeof(RBuf));	
	ret=PkgQueue_Read(ptmp,sizeof(RBuf),&RBuf[0]);
	UT_CHECK(ret == dLen);	
	Log_ShowArray(NULL,ret,&RBuf[0]);
	
	
	PkgQueue_ShowObj(ptmp);
	#endif
	
	#if 1
	LLOG("\n====== Test Loop RW\n");
	// PkgQueue_Reset(ptmp);
	dLen = 15;
	
	
	ret = PkgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	
	MEMSET(&RBuf[0],0,sizeof(RBuf));	
	ret=PkgQueue_Read(ptmp,sizeof(RBuf),&RBuf[0]);
	UT_CHECK(ret == dLen);	
	Log_ShowArray(NULL,ret,&RBuf[0]);

	PkgQueue_ShowObj(ptmp);
	
	
	
	#endif
	
	#if 1
	LLOG("\n====== Test Loop RW-2\n");
	PkgQueue_Reset(ptmp);
	dLen = 7;
	
	for(i=0;i<6;i++){
		ret = PkgQueue_Write(ptmp,dLen,&TestStr[0]);
		UT_CHECK(ret == dLen);
		
		MEMSET(&RBuf[0],0,sizeof(RBuf));	
		ret=PkgQueue_Read(ptmp,sizeof(RBuf),&RBuf[0]);
		UT_CHECK(ret == dLen);	
		Log_ShowArray(NULL,ret,&RBuf[0]);
		PkgQueue_ShowObj(ptmp);
		LLOG("\n======\n");
	}
	#endif	
	
	
	return ret;
}

#define TEST_RBUF_SZ_LONG 500
uint8_t g_TestStr[TEST_RBUF_SZ_LONG];
uint8_t g_RBuf[TEST_RBUF_SZ_LONG];

// #define UT_READ(RNUM) do{\
	// MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));\
	// ret = PkgQueue_Read(ptmp,(RNUM),&g_RBuf[0]);\
	// UT_CHECK(ret == dLen);\
	// Log_ShowArray(NULL,16,&g_RBuf[0]);\
	// Log_ShowArray(NULL,16,&g_RBuf[ret-16-1]);\
// }while(0);

#define UT_READ(RNUM) do{\
	MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));\
	ret = PkgQueue_Read(ptmp,(RNUM),&g_RBuf[0]);\
	LLOG("rRet=[%d]\n",ret);\
	UT_CHECK(ret == dLen);\
	if(ret == dLen){\
		Log_ShowArray(NULL,16,&g_RBuf[0]);\
	}\
}while(0);


int UT_PkgQueue_Long(void){
	
	uint32_t ret;

	uint32_t dLen;
	uint32_t RealNum;
	tPkgQueue* ptmp;
	
	int i;
	
	LLOG_MARK();
	ptmp = PkgQueue_Create(TEST_RBUF_SZ_LONG);
	PkgQueue_ShowObj(ptmp);
	
	for(i=0;i<TEST_RBUF_SZ_LONG;i++){
		g_TestStr[i] = i;
	}
	
	#if 1
	LLOG("\n====== Test single RW-127\n");
	PkgQueue_Reset(ptmp);
	dLen = 127;
	
	ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
	LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == dLen);
	
	UT_READ(sizeof(g_RBuf));
	
	PkgQueue_ShowObj(ptmp);
	#endif
	
	#if 0
	LLOG("\n====== Test single RW-128\n");
	PkgQueue_Reset(ptmp);
	dLen = 128;
	
	ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
	LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == dLen);
	PkgQueue_ShowObj(ptmp);
	LLOG("\n======\n");
	UT_READ(sizeof(g_RBuf));
	
	PkgQueue_ShowObj(ptmp);
	#endif	

	#if 0
	LLOG("\n====== Test single RW-300\n");
	// PkgQueue_Reset(ptmp);
	dLen = 300;
	
	ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
	UT_CHECK(ret == dLen);
	
	PkgQueue_ShowObj(ptmp);
	
	UT_READ(sizeof(g_RBuf));
	
	PkgQueue_ShowObj(ptmp);
	#endif		
	
	#if 0
	LLOG("\n====== Test loop RW-150\n");
	PkgQueue_Reset(ptmp);
	dLen = 150;
	
	for(i=0;i<6;i++){
		ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
		LLOG("wRet=[%d]\n",ret);
		UT_CHECK(ret == dLen);
		// PkgQueue_ShowObj(ptmp);
		MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));
		ret = PkgQueue_Read(ptmp,sizeof(g_RBuf),&g_RBuf[0]);
		LLOG("rRet=[%d]\n",ret);
		UT_CHECK(ret == dLen);
		
		PkgQueue_ShowObj(ptmp);
		LLOG("\n======\n");
	}
	#endif	

	#if 0 //
	LLOG("\n====== Test len not clean\n");
	PkgQueue_Reset(ptmp);
	dLen = 248;
	ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
	LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == dLen);
	Log_ShowArray("wLen",2,ptmp->pBuf);
	
	dLen = 247;
	ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
	LLOG("wRet=[%d]\n",ret);
	UT_CHECK(ret == dLen);
	PkgQueue_ShowObj(ptmp);
	LLOG("\n======\n");


	MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));
	ret = PkgQueue_Read(ptmp,sizeof(g_RBuf),&g_RBuf[0]);
	LLOG("rRet=[%d]\n",ret);
	UT_CHECK(ret == 248);
	
	Log_ShowArray("wLen after read",2,ptmp->pBuf);
	
	PkgQueue_ShowObj(ptmp);
	LLOG("\n======\n");	
	
	MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));
	ret = PkgQueue_Read(ptmp,sizeof(g_RBuf),&g_RBuf[0]);
	LLOG("rRet=[%d]\n",ret);
	UT_CHECK(ret == 247);
	PkgQueue_ShowObj(ptmp);
	LLOG("\n======\n");			
	
	for(i=0;i<3;i++){
		MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));
		ret = PkgQueue_Read(ptmp,sizeof(g_RBuf),&g_RBuf[0]);
		LLOG("rRet=[%d]\n",ret);
		UT_CHECK(ret == 0);
		// PkgQueue_ShowObj(ptmp);
		LLOG("\n======\n");	
	}
	
	#endif		
	
	#if 1 // u8 / u16 混合 
	do{
		uint8_t tdlen[]={16,128,17,130,128,8};
		
		LLOG("\n====== Test u8Len and u16 len\n");
		PkgQueue_Reset(ptmp);
		for(i=0;i<sizeof(tdlen);i++){
			dLen = tdlen[i];
			ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
			LLOG("wRet=[%d]\n",ret);
			UT_CHECK(ret == dLen);
			// PkgQueue_ShowObj(ptmp);
			MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));
			ret = PkgQueue_Read(ptmp,sizeof(g_RBuf),&g_RBuf[0]);
			LLOG("rRet=[%d]\n",ret);
			UT_CHECK(ret == dLen);
			PkgQueue_ShowObj(ptmp);
			LLOG("\n======\n");
		}	
		
	}while(0);
	#endif
	
	#if 1 // u8 / u16 混合 
	do{
		uint8_t tdlen[]={16,128,17,130,128,8};
		int j;
		LLOG("\n====== Test u8Len and u16 len 2\n");
		PkgQueue_Reset(ptmp);
		
		for(j=0;j<2;j++){
			for(i=0;i<sizeof(tdlen);i++){
				dLen = tdlen[i];
				ret = PkgQueue_Write(ptmp,dLen,&g_TestStr[0]);
				LLOG("wRet=[%d]\n",ret);
				UT_CHECK(ret == dLen);
			}
			PkgQueue_ShowObj(ptmp);
			LLOG("\n======\n");
			for(i=0;i<sizeof(tdlen);i++){
				dLen = tdlen[i];
				// PkgQueue_ShowObj(ptmp);
				MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));
				ret = PkgQueue_Read(ptmp,sizeof(g_RBuf),&g_RBuf[0]);
				LLOG("rRet=[%d]\n",ret);
				UT_CHECK(ret == dLen);
				PkgQueue_ShowObj(ptmp);
				LLOG("\n======\n");
			}	
		}
	}while(0);
	#endif	
	return ret;
}


#endif

#ifdef CFG_UT_MSG_QUEUE

#define TEST_RBUF_SZ_LONG 500
static uint8_t g_TestStr[TEST_RBUF_SZ_LONG];
static uint8_t g_RBuf[TEST_RBUF_SZ_LONG];

#define UT_READ(RNUM) do{\
	MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));\
	ret = MsgQueue_Read(ptmp,(RNUM),&g_RBuf[0]);\
	UT_CHECK(ret == dLen);\
	if(ret != dLen){\
		LLOG("rRet = [%d]\n",ret);\
		if(ret > 16){\
			Log_ShowArray(NULL,16,&g_RBuf[0]);\
		} else{\
			Log_ShowArray(NULL,ret,&g_RBuf[0]);\
		}\
	}\
}while(0);

#define UT_PEEK(RNUM) do{\
	MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));\
	ret = MsgQueue_Peek(ptmp,(RNUM),&g_RBuf[0]);\
	UT_CHECK(ret == dLen);\
	if(ret != dLen){\
		LLOG("rRet = [%d]\n",ret);\
		if(ret > 16){\
			Log_ShowArray(NULL,16,&g_RBuf[0]);\
		} else{\
			Log_ShowArray(NULL,ret,&g_RBuf[0]);\
		}\
	}\
}while(0);

int UT_MsgQueue(void){
	
	uint32_t ret;
	uint8_t TestStr[]="0123456789abcdefghijklmn";
	uint32_t dLen;
	uint32_t RealNum;
	tMsgQueue* ptmp;
	
	int i;
	
	LLOG_MARK();
	// MemHeap_ShowBiMap();
	ptmp = MsgQueue_Create(10); // 
	MsgQueue_ShowObj(ptmp);
	
	MemHeap_ShowBiMap();
	
	#if 1
	LLOG("\n====== Test single RW\n");
	MsgQueue_Reset(ptmp);
	dLen = 8;
	
	ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	
	UT_READ(sizeof(g_RBuf));
	UT_CHECK( CMP_OK == MEMCMP(&g_RBuf[0],&TestStr[0],dLen));

	// MsgQueue_ShowObj(ptmp);
	#endif
	
	#if 1
	LLOG("\n====== Test single RW2\n");
	do{
		uint8_t DLenbuf[]={8,9,10,11,20};
		for(i=0;i<sizeof(DLenbuf);i++){
			dLen = DLenbuf[i];
			ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
			UT_CHECK(ret == dLen);
			UT_CHECK(dLen == MsgQueue_GetNextPkgSz(ptmp));
			UT_READ(sizeof(g_RBuf));
			UT_CHECK( CMP_OK == MEMCMP(&g_RBuf[0],&TestStr[0],dLen));			
		}
		MsgQueue_ShowObj(ptmp);
	}while(0);
	
	LLOG("======\n");
 	ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	MsgQueue_ShowObj(ptmp);
	
	// MemHeap_ShowBiMap();
	#endif
	
	
	#if 1
	LLOG("\n====== Test Loop RW-2\n");
	MsgQueue_Reset(ptmp);
	dLen = 7;
	for(i=0;i<14;i++){
		ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
		UT_CHECK(ret == dLen);
		
		UT_READ(sizeof(g_RBuf));
		UT_CHECK( CMP_OK == MEMCMP(&g_RBuf[0],&TestStr[0],dLen));
	}

	#endif
	#if 1
	LLOG("\n====== Test Over Write\n");
	for(i=0;i<14;i++){
		ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
		UT_CHECK(ret == dLen);
	}	
	MsgQueue_ShowObj(ptmp);
	MsgQueue_Reset(ptmp);
	#endif
	
	#if 1
	LLOG("\n====== Empty Queue Read\n");
	UT_READ(sizeof(g_RBuf));
		
	#endif	
	
	#if 1
	LLOG("\n====== Peek\n");
	dLen = 10;
	ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);

	dLen = 10;
	ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	
	UT_PEEK(sizeof(g_RBuf));
	MsgQueue_ShowObj(ptmp);
	
	UT_READ(sizeof(g_RBuf));
	UT_READ(sizeof(g_RBuf));
	
	#endif		
	#if 1
	LLOG("\n====== DropOne\n");
	dLen = 10;
	ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);

	dLen = 10;
	ret = MsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	MsgQueue_DorpOne(ptmp);
	
	MsgQueue_ShowObj(ptmp);
	
	UT_READ(sizeof(g_RBuf));		
	MsgQueue_ShowObj(ptmp);
	#endif	
	
	LLOG("\n====== Check memory leakage \n");
	MsgQueue_ShowObj(ptmp);
	MemHeap_ShowBiMap();
	
	return ret;
}

#endif 

#ifdef CFG_UT_DLMSG_QUEUE
#define TEST_RBUF_SZ_LONG 500
static uint8_t g_TestStr[TEST_RBUF_SZ_LONG];
static uint8_t g_RBuf[TEST_RBUF_SZ_LONG];

#define UT_READ(RNUM) do{\
	MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));\
	ret = DLMsgQueue_Read(ptmp,(RNUM),&g_RBuf[0]);\
	UT_CHECK(ret == dLen);\
	if(ret != dLen){\
		LLOG("rRet = [%d]\n",ret);\
		if(ret > 16){\
			Log_ShowArray(NULL,16,&g_RBuf[0]);\
		} else{\
			Log_ShowArray(NULL,ret,&g_RBuf[0]);\
		}\
	}\
}while(0);

#define UT_PEEK(RNUM) do{\
	MEMSET(&g_RBuf[0],0,sizeof(g_RBuf));\
	ret = DLMsgQueue_Peek(ptmp,(RNUM),&g_RBuf[0]);\
	UT_CHECK(ret == dLen);\
	if(ret != dLen){\
		LLOG("rRet = [%d]\n",ret);\
		if(ret > 16){\
			Log_ShowArray(NULL,16,&g_RBuf[0]);\
		} else{\
			Log_ShowArray(NULL,ret,&g_RBuf[0]);\
		}\
	}\
}while(0);

int UT_DLMsgQueue(void){
	
	uint32_t ret;
	uint8_t TestStr[]="0123456789abcdefghijklmn";
	uint32_t dLen;
	uint32_t RealNum;
	tDLMsgQueue* ptmp;
	
	int i;
	
	LLOG_MARK();
	// MemHeap_ShowBiMap();
	ptmp = DLMsgQueue_Create(10); // if 0 ,not limit item num  
	DLMsgQueue_ShowObj(ptmp);
	
	MemHeap_ShowBiMap();
	
	#if 1
	LLOG("\n====== Test single RW\n");
	DLMsgQueue_Reset(ptmp);
	dLen = 8;
	
	ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	// MemHeap_ShowBiMap();
	
	UT_READ(sizeof(g_RBuf));
	UT_CHECK( CMP_OK == MEMCMP(&g_RBuf[0],&TestStr[0],dLen));

	// DLMsgQueue_ShowObj(ptmp);
	#endif
	
	#if 1
	LLOG("\n====== Test single RW2\n");
	do{
		uint8_t DLenbuf[]={8,9,10,11,20};
		for(i=0;i<sizeof(DLenbuf);i++){
			dLen = DLenbuf[i];
			ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
			UT_CHECK(ret == dLen);
			UT_CHECK(dLen == DLMsgQueue_GetNextPkgSz(ptmp));
			UT_READ(sizeof(g_RBuf));
			UT_CHECK( CMP_OK == MEMCMP(&g_RBuf[0],&TestStr[0],dLen));			
		}
		// DLMsgQueue_ShowObj(ptmp);
	}while(0);
	
	LLOG("======\n");
 	ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	// DLMsgQueue_ShowObj(ptmp);

	// LLOG("======\n");
 	ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	
	DLMsgQueue_ShowObj(ptmp);
	MemHeap_ShowBiMap();
	#endif
	
	#if 1
	LLOG("\n====== Test Loop RW-2\n");
	DLMsgQueue_Reset(ptmp);
	dLen = 7;
	for(i=0;i<14;i++){
		ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
		UT_CHECK(ret == dLen);
		
		UT_READ(sizeof(g_RBuf));
		UT_CHECK( CMP_OK == MEMCMP(&g_RBuf[0],&TestStr[0],dLen));
	}
	
	for(i=0;i<17;i++){
		ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
		UT_CHECK(ret == dLen);
	}
	MemHeap_ShowBiMap();
	DLMsgQueue_ShowObj(ptmp);
	
	DLMsgQueue_Reset(ptmp);	
	#endif
	
	#if 1
	LLOG("\n====== Empty Queue Read\n");
	UT_READ(sizeof(g_RBuf));
		
	#endif

	#if 1
	LLOG("\n====== Peek\n");
	dLen = 10;
	ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);

	dLen = 10;
	ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	
	UT_PEEK(sizeof(g_RBuf));
	DLMsgQueue_ShowObj(ptmp);
	
	UT_READ(sizeof(g_RBuf));
	UT_READ(sizeof(g_RBuf));
		
	#endif	
	
	#if 1
	LLOG("\n====== DropOne\n");
	dLen = 10;
	ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);

	dLen = 10;
	ret = DLMsgQueue_Write(ptmp,dLen,&TestStr[0]);
	UT_CHECK(ret == dLen);
	DLMsgQueue_DorpOne(ptmp);
	
	DLMsgQueue_ShowObj(ptmp);
	
	UT_READ(sizeof(g_RBuf));		
	DLMsgQueue_ShowObj(ptmp);
	#endif	
		
	
	
	LLOG("\n====== Check memory leakage \n");
	DLMsgQueue_ShowObj(ptmp);
	MemHeap_ShowBiMap();

	return ret;
}

#endif

//
#ifdef CFG_UT_OBJ_POOL		// UintTest
typedef struct CC_Test_AA{
	uint8_t 	A;
	// uint8_t C;
	uint16_t 	B;
}tAA;

#define UT_OBJPOOL_NUMSZ 20
int UT_ObjPool(void){
	
	int i;
	int ret=RET_OK;
	tObjPool* pObj=NULL;
	
	tAA* pBuf[UT_OBJPOOL_NUMSZ];
	tAA* ptmp=NULL;
	
	MEMSET((uint8_t*)pBuf,0,sizeof(tAA));
	pObj=ObjPool_Create(sizeof(tAA),UT_OBJPOOL_NUMSZ);
	if(pObj == NULL){
		LLOG_ERR();
		return ret;
	}
	ObjPool_Show(NULL,pObj);
	
	// 1-测试正常获取:释放 单个
	i=0;
	pBuf[i]=(tAA*)ObjPool_TakeOne(pObj);
	ObjPool_ShowBMap("Alloc 1",pObj);
	
	
	pBuf[i]->A=1;
	ret=ObjPool_Release(pObj,pBuf[i]);
	LLOG("#Release index[%d]\n",ret);
	ObjPool_ShowBMap("Release 1",pObj);

	
	// 2-测试正常获取:获取3个 释放中间的
	for(i=0;i<3;i++){
		pBuf[i]=(tAA*)ObjPool_TakeOne(pObj);
		pBuf[i]->A=i+1;
	}
	ObjPool_ShowBMap("Step2 alloc",pObj);
	ret=ObjPool_Release(pObj,pBuf[1]);
	ObjPool_ShowBMap("Step2 release",pObj);
	
	
	// 3-测试正常获取: 再分配3个，释放一个
	for(;i<3+3;i++){
		pBuf[i]=(tAA*)ObjPool_TakeOne(pObj);
		pBuf[i]->A=i+1;
	}	
	ObjPool_ShowBMap("Step3 alloc",pObj);
	ret=ObjPool_Release(pObj,pBuf[0]);
	ObjPool_ShowBMap("Step3 release",pObj);
	
	//4-测试正常获取: 释放所有数据
	for(i=0;i<UT_OBJPOOL_NUMSZ;i++){
		ret=ObjPool_Release(pObj,pBuf[i]);
		DBG("#Release %d =index[%d]\n",i,ret);
	}	
	ObjPool_ShowBMap("Step4",pObj);
	#if 1
	//5-测试正常获取: 分配所有数据
	for(i=0;i<UT_OBJPOOL_NUMSZ+2;i++){
		
		ptmp=(tAA*)ObjPool_TakeOne(pObj);
		if(ptmp!=NULL){
			pBuf[i]=ptmp;
		}
	}
	ObjPool_ShowBMap("Step5",pObj);
	ObjPool_Show("Step5",pObj);
	#endif
	
}

#endif

//
#ifdef CFG_UT_TBMAP				// TwoBitMap // D200616 

#define TBM_UT_BUFSZ 5
static uint8_t g_TBMapBuf[TBM_UT_BUFSZ];	// 20
static void TBMap_ShowBuf(uint8_t* pStr){
	if(pStr!=NULL){
		DBG("%s\n",pStr);
	}		
	Log_ShowArray("TBM:",sizeof(g_TBMapBuf),&g_TBMapBuf[0]);
}

int UT_TBMap(void){ // Pass -D200626
	
	int ret=RET_ERR;
	int i;
	uint16_t BufSz;
	uint8_t* pBuf;
	int index;
	int IndBuf[20];
	
	// Init
	BufSz	=sizeof(g_TBMapBuf);
	pBuf	=&g_TBMapBuf[0];
	MEMSET(pBuf,0,BufSz);
	MEMSET((uint8_t*)&IndBuf[0],0,sizeof(IndBuf)); // 测试输入随机乱码index的行为
	TBMap_ShowBuf("Init");
	
	
	// 1- 分配-释放 单对操作
	i=0;
	IndBuf[i]=TBMap_Alloc(BufSz,pBuf,9);
	TBMap_ShowBuf("Step1 Alloc");
	index=TBMap_CleanByIndex(BufSz,pBuf,IndBuf[i]);
	TBMap_ShowBuf("Step1 Free");
	
	
	// 2- 分配-释放 多个分配+单个释放 操作
	i=0;
	for(i=0;i<3;i++){
		IndBuf[i]=TBMap_Alloc(BufSz,pBuf,3);
	}
	TBMap_ShowBuf("Step2 Alloc");
	index=TBMap_CleanByIndex(BufSz,pBuf,IndBuf[1]);
	LLOG("Step2 Free 2ndSz[%d]\n",index);
	TBMap_ShowBuf(NULL);
	
	
	// 3- 分配-释放 再多个分配+单个释放 操作
	for(;i<3+3;i++){
		IndBuf[i]=TBMap_Alloc(BufSz,pBuf,3);
	}
	TBMap_ShowBuf("Step3 Alloc");
	index=TBMap_CleanByIndex(BufSz,pBuf,IndBuf[0]);
	LLOG("Step3 Free 1ndSz[%d]\n",index);
	TBMap_ShowBuf(NULL);
	
	// 4- 分配-释放 释放所有
	i=0;
	for(i=0;i<20;i++){
		index=TBMap_CleanByIndex(BufSz,pBuf,IndBuf[i]);
		LLOG(" %d=[%d]",i,IndBuf[i]);
		LLOG(" ind=[%d]\n",index);
	}
	TBMap_ShowBuf("Step4 Free All");
	
	// 5- 分配-释放 过度分配
	i=0;
	for(i=0;i<20;i++){
		IndBuf[i]=TBMap_Alloc(BufSz,pBuf,i);
	}
	TBMap_ShowBuf("Step5 Alloc");
	
	LLOG("Step5 IndBuf:\n");
	i=0;
	for(i=0;i<20;i++){
		LLOG(" %d:Ind[%d]\n",i,IndBuf[i]);
	}	
	
	IndBuf[7]=TBMap_Alloc(BufSz,pBuf,5);
	TBMap_ShowBuf("Step5 Full");
	
	
	// 6- 强制全部清除
	TBMap_ForceClean(BufSz,pBuf,0,BufSz*4);
	TBMap_ShowBuf("Step6 Release All");
	#if 1
	// 7- 分配-释放 留空分配尝试
	MEMSET((uint8_t*)&IndBuf[0],0,sizeof(IndBuf));
	for(i=1;i<20;i++){
		index=TBMap_Alloc(BufSz,pBuf,i);
		if(index < 0){
			break;
		}
		else{
			IndBuf[i]=index;
		}
	}

	// LLOG("Step7 IndBuf:\n");
	// i=0;
	// for(i=0;i<20;i++){
		// LLOG(" %d:Ind[%d]\n",i,IndBuf[i]);
	// }
	
	TBMap_ShowBuf("Step7 Alloc");
	
	index=TBMap_CleanByIndex(BufSz,pBuf,IndBuf[3]);
	LLOG("Step7 Free Sz[%d]\n",index);
	TBMap_ShowBuf(NULL);
	index=TBMap_Alloc(BufSz,pBuf,4);
	LLOG("Step7 alloc4 index[%d]\n",index); TBMap_ShowBuf(NULL);
	index=TBMap_Alloc(BufSz,pBuf,2);
	LLOG("Step7 alloc2 index[%d]\n",index); TBMap_ShowBuf(NULL);
	index=TBMap_Alloc(BufSz,pBuf,1);
	LLOG("Step7 alloc1 index[%d]\n",index); TBMap_ShowBuf(NULL);
	index=TBMap_Alloc(BufSz,pBuf,5);
	LLOG("Step7 alloc5 index[%d]\n",index);	TBMap_ShowBuf(NULL);
	// TBMap_ShowBuf("Step7 End");
	
	#endif
	return ret;
}

#endif

#ifdef CFG_UT_DYNAMIC_ALLOCATE		// D200616 


extern int BlkPool_CmpBMap(tBlkPool* pObj,uint8_t* pCmpBuf);
static uint8_t g_Cmp_2Block[]={ 0xb0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15 };

static uint8_t g_Cmp_Empty[]={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15};

static uint8_t g_Cmp_6Block[]={0x5b, 0xbb, 0xef, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15};


static uint8_t g_Cmp_r6Block[]={0x6e, 0xef, 0xff, 0xff, 0xbf, 0xff, 0xbf, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15};

static uint8_t g_Cmp_r6Block_free2[]={0x42, 0xef, 0xff, 0xff, 0xbf, 0xff, 0xbf, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15};

static uint8_t g_Cmp_25Block[]={0xbd, 0xbb, 0xbf, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xff, 0xfe, 0xff, 0xee, 0xff, 0xfb, 0xff, 0xbf, 0xef, 0xff, 0xbf, 0xff, 0xff, 0xbe, 0xff, 0xfe, 0xdb, 0xd5};
// 
#define UT_CHECK_BITMAP(CMP_DATA)	do{\
	if(!(BlkPool_CmpBMap(pObj,(CMP_DATA)) == RET_OK)){\
		BlkPool_ShowBMap("Err Bitmap",pObj);\
		LLOG_ERR();\
	}\
}while(0);

// #define UT_BLKPOOL_NUMSZ 512
#define UT_BLKPOOL_NUMSZ 100+1
int UT_BlkPool(void){
	/***
		1- 为对具体对象 空间进行过测试 
	*/ 
	int ret=RET_ERR;
	int i,j;
	int RandomNum;

	tBlkPool* pObj;
	int index;
	uint8_t* pBuf;
	uint8_t* PntrBuf[30];

	uint8_t* pRBuf;
	uint16_t RBufSz = UT_BLKPOOL_NUMSZ;
	
	MEMSET((uint8_t*)&PntrBuf[0],0,sizeof(PntrBuf)); // 
	// Init
	pObj = BlkPool_Create(8,UT_BLKPOOL_NUMSZ);
	BlkPool_Show("Init",pObj);
	BlkPool_ShowBMap("Init",pObj);
	
	#if 1 	// 1- 单对操作
	LLOG("\n==== ==== ==== Malloc 2Block 0b1011 0000 \n");
	do{ 
		i=0;
		PntrBuf[i]=BlkPool_DynAlloc(pObj,9);
		// BlkPool_ShowBMap("Step1 Alloc",pObj);
		// UT_CHECK(BlkPool_CmpBMap(pObj,g_Cmp_2Block) == RET_OK);
		UT_CHECK_BITMAP(g_Cmp_2Block);

		
		index=BlkPool_Release(pObj,PntrBuf[i]);
		UT_CHECK_BITMAP(g_Cmp_Empty);
	}while(0);
	#endif
	
	#if 1 	// 2- 分配单个区域or多个区域大小空间
	LLOG("\n==== ==== ==== Malloc 6 Free all\n");
	do{ 
		i=0;
		PntrBuf[i++]=BlkPool_DynAlloc(pObj,7); //01011011 10111011 11000000
		PntrBuf[i++]=BlkPool_DynAlloc(pObj,8);
		PntrBuf[i++]=BlkPool_DynAlloc(pObj,9);
		PntrBuf[i++]=BlkPool_DynAlloc(pObj,16);
		PntrBuf[i++]=BlkPool_DynAlloc(pObj,17);
		PntrBuf[i++]=BlkPool_DynAlloc(pObj,24);//0101 1011 1011 1011 11101111
		UT_CHECK_BITMAP(g_Cmp_6Block);

		j=i;
		for(i=0;i<j;i++){
			if(PntrBuf[i] == NULL){
				LLOG_ERR();
			}
			else{
				ret = BlkPool_Release(pObj,PntrBuf[i]);
				if(ret == RET_ERR){
					LLOG_ERR();
				}
			}
		}
		UT_CHECK_BITMAP(g_Cmp_Empty);
	}while(0);
	#endif
	
	#if 1	// 3- 分配-释放 多个随机分配+单个释放+全部释放 操作
	LLOG("\n==== ==== ==== Malloc6 Free1 Free-all\n");
	do{
		uint8_t AllocNum[]={4,14,15,85,63,92};
		i=0;
		j=6;
		for(i=0;i<j;i++){
			pBuf=BlkPool_DynAlloc(pObj,AllocNum[i]);
			if(pBuf != NULL){
				PntrBuf[i]=pBuf;
			}
			else{
				PntrBuf[i]=NULL;
			}
		}
		UT_CHECK_BITMAP(g_Cmp_r6Block);
	
		BlkPool_Release(pObj,PntrBuf[1]);
		UT_CHECK_BITMAP(g_Cmp_r6Block_free2);

		for(i=0;i<j;i++){
			BlkPool_Release(pObj,PntrBuf[i]);	
		}
		UT_CHECK_BITMAP(g_Cmp_Empty);
	}while(0);
	
	#endif
	
	#if 1	// 4- 分配-释放 过度分配
	/*
0  Num[21] Pntr[40a0ac]
1  Num[4	] Pntr[40a0c4]
2  Num[14] Pntr[40a0cc]
3  Num[15] Pntr[40a0dc]
4  Num[85] Pntr[40a0ec]
5  Num[63] Pntr[40a144]
6  Num[92] Pntr[40a184]
7  Num[44] Pntr[40a1e4]
8  Num[16] Pntr[40a214]
9  Num[55] Pntr[40a224]
10 Num[43] Pntr[40a25c]
11 Num[34] Pntr[40a28c]
12 Num[56] Pntr[40a2b4]
13 Num[93] Pntr[40a2ec]
14 Num[20] Pntr[40a34c]
15 Num[60] Pntr[40a364]
16 Num[72] Pntr[0]
17 Num[13] Pntr[40a3a4]
18 Num[5	] Pntr[40a3b4]
19 Num[19] Pntr[40a3bc]
20 Num[57] Pntr[0]
21 Num[74] Pntr[0]
22 Num[1	] Pntr[0]
23 Num[36] Pntr[0]
24 Num[28] Pntr[0]
	
	*/
	do{ // Q-建立一个随机分配-释放的机制，句柄总数限制;
		int SumIndex;
		LLOG("\n==== ==== ==== Step4 Full \n");
		uint8_t AllocNum[]={21, 4, 14, 15, 85, 63, 92, 44, 16, 55, 43, 34, 56, 93, 20, 60, 72, 13, 5, 19, 57, 74, 1, 36, 28};
		i=0;
		j=25;
		for(i=0;i<j;i++){
			// RandomNum=CC_Random(2,100);
			pBuf=BlkPool_DynAlloc(pObj,AllocNum[i]);
			if(pBuf != NULL){
				PntrBuf[i]=pBuf;
			}
			else{
				PntrBuf[i]=NULL;
			}
			// LLOG("%d Num[%d] Pntr[%x]\n",i,RandomNum,pBuf);
		}
		UT_CHECK_BITMAP(g_Cmp_25Block);
 
		// PntrBuf[i]=BlkPool_DynAlloc(pObj,4);
		// BlkPool_Release(pObj,PntrBuf[i]);
		//
		SumIndex = 0;
		BlkPool_ShowBMap("Step4 Alloc",pObj);
		for(i=0;i<j;i++){
			index = BlkPool_Release(pObj,PntrBuf[i]);
			if(index >0){
				SumIndex+=index;
			}
			// LLOG(" %d ReleaseNum[%d]\n",i,index);
		}
		LLOG("SumIndex=[%d]\n",SumIndex);
		UT_CHECK_BITMAP(g_Cmp_Empty);
	}while(0);
	#endif

	#if 1 	// Malloc_RandomTest
	LLOG("\n==== ==== ==== Malloc_RandomTest\n");
	do{	
		int cnt;
		UT_CHECK_BITMAP(g_Cmp_Empty);
	
		RandomNum = CC_Random(2,CC_DIM(PntrBuf)-1);
		
		uint8_t AllocNum[] = {21, 4, 14, 15, 85, 63, 92, 44, 16, 55, 43, 34, 56, 93, 20, 60, 72, 13, 5, 19, 57, 74, 1, 36, 28};
		// N- 首先填充满 buf
		i=0;
		j=25;
		for(i=0;i<j;i++){
			pBuf=BlkPool_DynAlloc(pObj,AllocNum[i]);
			if(pBuf != NULL){
				PntrBuf[i]=pBuf;
			}
			else{
				PntrBuf[i] = NULL;
			}
		}
		UT_CHECK_BITMAP(g_Cmp_25Block);
		
		LLOG("\n==== ==== ==== Random Loop 100\n");
		for(j=0;j<100;j++){
			// N-随机 释放一个 指针句柄
			RandomNum = CC_Random(2,CC_DIM(PntrBuf));
			if(PntrBuf[RandomNum] != NULL){
				BlkPool_Release(pObj,PntrBuf[RandomNum]);
				PntrBuf[RandomNum] = 0;
			}
			// 查找一个空的句柄;
		 	RandomNum = -1;
			for(i=0;i<CC_DIM(PntrBuf);i++){
				if(PntrBuf[i] == NULL){
					RandomNum = i;
					break;
				}
			}
			
			if(RandomNum < 0){
				LLOG_ERR();
			}
			else {
				// N-随机 申请一段 数据块
				PntrBuf[RandomNum]=BlkPool_DynAlloc(pObj,CC_Random(2,100));				
			}
		}
		LLOG("Loop j %d \n",j);
		BlkPool_ShowBMap("just Random",pObj);
		// 释放所有 空间 
		cnt=0;
		for(i=0;i<CC_DIM(PntrBuf);i++){
			if( PntrBuf[i] != NULL){
				ret=BlkPool_Release(pObj,PntrBuf[i]);
				cnt++;
			}
			// LLOG("PntrBuf[%i] == 0x[%x]\n",i,PntrBuf[i]);
		}
		LLOG("cnt = %d\n",cnt);
		UT_CHECK_BITMAP(g_Cmp_Empty);		
	}while(0);
	#endif
	
	#if 1 	// Malloc Full 
	LLOG("\n==== ==== ==== Malloc_Full\n");
	do{
		int cnt = 20;
		for(i=0;i<CC_DIM(PntrBuf);i++){
			pBuf=BlkPool_DynAlloc(pObj,cnt++);
			if(pBuf != NULL){
				PntrBuf[i]=pBuf;
			}
			else{
				break;
			}			
		}
		LLOG("cnt = %d,i=%d \n",cnt,i);
		for(;i<CC_DIM(PntrBuf);i++){
			pBuf=BlkPool_DynAlloc(pObj,8);
			if(pBuf != NULL){
				PntrBuf[i]=pBuf;
			}
			else{
				break;
			}
		}
		LLOG("cnt = %d,i=%d \n",cnt,i);
		BlkPool_ShowBMap("Full ",pObj);
		cnt=0;
		
		for(i=0;i<CC_DIM(PntrBuf);i++){
			if( PntrBuf[i] != NULL){
				ret = BlkPool_Release(pObj,PntrBuf[i]);
				cnt++;
			}
			// LLOG("PntrBuf[%i] == 0x[%x]\n",i,PntrBuf[i]);
		}
		UT_CHECK_BITMAP(g_Cmp_Empty);		
	}while(0);
	#endif
	
	#if 0// 6- 分配-释放 间隔释放后再分配 模拟一般场景  
	LLOG("\n==== ==== ==== Step6\n");
	
	i=0;
	j=25;
	for(i=0;i<j;i++){
		RandomNum=CC_Random(2,100);
		pBuf=BlkPool_DynAlloc(pObj,RandomNum);
		if(pBuf != NULL){
			PntrBuf[i]=pBuf;
		}
		else{
			PntrBuf[i]=NULL;
		}
		LLOG("%d Num[%d] Pntr[%x]\n",i,RandomNum,pBuf);
	}
	BlkPool_ShowBMap("Step5 Alloc",pObj);

	for(i=0;i<j;){
		index=BlkPool_Release(pObj,PntrBuf[i]);	
		// LLOG(" %d ReleaseNum[%d]\n",i,index);
		i+=3;
	}
	BlkPool_ShowBMap("Step5 Free Part",pObj);
	
	i=0;
	PntrBuf[i]=BlkPool_DynAlloc(pObj,4); i+=3;
	PntrBuf[i]=BlkPool_DynAlloc(pObj,20); i+=3;
	PntrBuf[i]=BlkPool_DynAlloc(pObj,24); i+=3;
	PntrBuf[i]=BlkPool_DynAlloc(pObj,50); i+=3;
	BlkPool_ShowBMap("Step5 Alloc Again",pObj);

	for(i=0;i<j;i++){
		index=BlkPool_Release(pObj,PntrBuf[i]);	
		// LLOG(" %d ReleaseNum[%d]\n",i,index);
	}
	BlkPool_ShowBMap("Step5 Free All",pObj);	
	#endif
	#if 0 // 7-
	i=0;
	PntrBuf[i]=BlkPool_DynAlloc(pObj,20);
	for(j=0;j<20;j++){
		PntrBuf[i][j]=j;
	}
	
	i=1;
	PntrBuf[i]=BlkPool_DynAlloc(pObj,30);
	MEMSET(PntrBuf[i],0x5A,30);
	
	BlkPool_ShowBMap("Step6 Alloc",pObj);
	#if 1
	Log_ShowArray("A:",20+4+4,PntrBuf[0]);
	Log_ShowArray("B:",30+2+4,PntrBuf[1]);
	#endif
	#endif
	
	return ret;	
}

#endif

#ifdef CFG_UT_APP_SORT_TIMER

static tSTimer* g_TimerSet[10];
static uint8_t g_Flag_RunEnd = 0;

static int Timer_A(void* pPara){
	
	int ret=RET_ERR;
	LLOG_TICK();
	return ret;
}

static int Timer_B(void* pPara){
	// 
	static uint16_t Cnt=0;
	int ret=RET_ERR;
	
	Cnt++;
	if( Cnt == 3 ){
		g_Flag_RunEnd = 1;
	}

	LLOG_TICK();
	return ret;
}

static int Timer_C(void* pPara){
	
	int ret=RET_ERR;
	LLOG_TICK();
	return ret;
}

static int UT_AppSortTimer(void){
	
	int ret=RET_ERR;
	int i;
	LLOG_MARK();
	SortTimer_Init();
	
	i=0;
	g_TimerSet[i++]=STimer_CreateAndAdd(0, STIMER_TYP_PERIOD, 1000);
	g_TimerSet[i++]=STimer_CreateAndAdd(0, STIMER_TYP_PERIOD, 1500);
	g_TimerSet[i++]=STimer_CreateAndAdd(0, STIMER_TYP_ONCE, 700);
	
	i=0;
	STimer_BindCBF(g_TimerSet[i++],Timer_A);
	STimer_BindCBF(g_TimerSet[i++],Timer_B);
	STimer_BindCBF(g_TimerSet[i++],Timer_C);
	
	i=0;
	STimer_OnOff(g_TimerSet[i++],1);
	STimer_OnOff(g_TimerSet[i++],1);
	STimer_OnOff(g_TimerSet[i++],1);
	
	i=0;
	STimer_Show(g_TimerSet[i++]);
	STimer_Show(g_TimerSet[i++]);
	STimer_Show(g_TimerSet[i++]);
	
	STimerX_Show();
	
	#if 1
	while(1){
		if(g_Flag_RunEnd == 1){
			break;
		}
		STimer_Run();
	}
	#endif
	
	
	return ret;
}

#endif

void UT_Case_Pub(void){
	
	#ifdef CFG_UT_MODE_FQUEUE
	UT_FQueue();
	#endif

	#ifdef CFG_UT_BYTE_FIFO
	UT_BFIFO();
	#endif

	#ifdef CFG_UT_PKG_QUEUE
	// UT_PkgQueue();
	UT_PkgQueue_Long();
	#endif	

	#ifdef CFG_UT_MSG_QUEUE
	UT_MsgQueue();
	#endif
	
	#ifdef CFG_UT_DLMSG_QUEUE
	UT_DLMsgQueue();
	#endif	
	
	#ifdef CFG_UT_OBJ_POOL
	
	#endif

	#ifdef CFG_UT_DYNAMIC_ALLOCATE
	UT_BlkPool();
	#endif
	
	#ifdef CFG_UT_GDLINK
	UT_GDLink();
	#endif

	#ifdef CFG_UT_APP_SORT_TIMER
	UT_AppSortTimer();
	#endif

}



